<?php
class Controller_App_Goods extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/goods_detail";
        parent::__construct($req, $res);
    }

    public function action_list()
    {
        $this->template->set_filename('smarty:app/goods_list');
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '商品列表', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 商品评论
    public function action_evaluation($goodsNid)
    {
        $pinglun = new App_Order_Pinglun();
        list($counts, $comments) = $pinglun->listByGoods($goodsNid, 0, 20);
        $this->template->set_filename('smarty:app/goods_evaluation');
        $this->template->set('commentsTotal', $counts);
        $this->template->set('commentsList',  $comments);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '宝贝评价', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 商品详情
    public function action_detail ($nid = 1)
    {
        $detail = App_Goods::detail($nid);

        if (! empty($detail))
        {
            App_History::add($this->_user, array('nid' => $nid));
            App_Goods::addCountsView($nid, 1);
        } 
        else
        {
            throw new Http_Exception_404();
        }

        $shop = App_Shop::basicInfo(Arr::get($detail, 'shopId'), FALSE);
        
        $this->template->set('shareTitle', App_Util::buildShareContent($detail['name']));
        $this->template->set('shareType', 'goods_detail');
//        $this->template->set('shareDesc', App_Util::buildShareContent($detail['shortDesc']));

        $this->template->set('goods', $detail);
        $this->template->set('shop',  $shop);
        $this->template->set('title', $detail['name']);
        $this->template->set('global_item', array(
            'nid' => $nid, 'type' => 'goods',
            'countsLike' => Arr::get($detail, 'countsLike', 0),
            'isLiked'    => (int) $this->_user->hasAction('goods', $nid, NULL),
            ));
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '宝贝详情', 'share', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);

        //
        // weixin
        //
        /*
        $this->template->set('weixin', $this->buildWeixinShareData($detail));

        $obj = new WxJssdk();
        $SignPackage = $obj->getSignPackage();
        $this->template->set('SignPackage', $SignPackage);
        */
    }

    // 拼单商品列表
    public function action_pindan ($page = 1)
    {
        $page  = max($page, 1);
        $limit = 10;
        $start = ($page - 1) * $limit;

        $goodsItems = App_Tuan::tuanGoodsList($start, $limit);

        $this->template->set_filename('smarty:app/tuan/list');
        $this->template->set('goodsItems', $goodsItems);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '拼单', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 团购商品(详情)入口
    public function action_tuan ($nid = 1)
    {
        $detail = App_Goods::detail($nid, 'tuan');
        App_Shop::setCurrentShopId(Arr::get($detail, 'shopId'));

        if (! empty($detail))
        {
            App_History::add($this->_user, array('nid' => $nid));
            App_Goods::addCountsView($nid, 1);
        } 

        $this->template->set('shareTitle', App_Util::buildShareContent($detail['name']));
        $this->template->set('shareType', 'tuan_detail');
        $this->template->set('goods', $detail);
        $this->template->set('title', $detail['name']);
        $this->template->set('global_item', array(
            'nid' => $nid, 'type' => 'goods',
            'countsLike' => Arr::get($detail, 'countsLike', 0),
            'isLiked'    => (int) $this->_user->hasAction('goods', $nid, NULL),
            ));
        $this->template->set('progress', App_Order_Tuan::progressStepStyle(NULL, $this->_uid));
        $this->template->set_filename("smarty:app/goods/tuan_detail");
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '团购详情', 'share', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    // 子分类下的商品列表
    // oderby = <countsView|changed|sales|price>
    public function action_cat ($subCatId, $page = 1)
    {
        $page  = max($page, 1);
        $limit = 10;
        $start = ($page - 1 ) * $limit;

        $order = Arr::get($this->request->query(), 'order', 'countsView');
        $result= App_Goods::search(NULL, "{$order}_desc", $start, $limit, $subCatId);
        $title = App_Category::tagName($subCatId);

        $this->template->set_filename('smarty:/app/goods/category_list');
        $this->template->set('goodsTotal', $result['total']);
        $this->template->set('goodsItems', $result['list']);
        $this->template->set('categoryId', $subCatId);
        $this->template->set('order', $order);
        $this->template->set('title', $title);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '分类', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }


    private function buildWeixinShareData($detail)
    {
        $url = url::site('/goods/detail/'.$detail['id'], 'http', FALSE);
        $url = rawurlencode($url);
        $uid = $this->_uid;
        $shopId = $detail['shopId'];

        return array(
            'title' => $detail['name'],
            'desc'  => $detail['shortDesc'],
            'imgUrl' => url::site($detail['thumbsrc'], 'http', FALSE),
            'link'   => url::site("/share/i/$uid/$shopId?url=$url", 'http', FALSE),
        );
    }
}
